package com.shuoyue.ycgk.views.dialog.pricedialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.entity.course_teacher.AppPrice;
import com.shuoyue.ycgk.ui.mine.vip.MyVipActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;
import com.shuoyue.ycgk.utils.XToast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 播放倍速选择Dialog
 * Created by Forty'7 on 2019/8/17
 */

public class PriceDialog extends Dialog {
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    private Context mContext;

    private PriceAdapter mAdapter;


    public interface SelectCall {
        void select(AppPrice speed);
    }

    public PriceDialog(@NonNull Context context, List<AppPrice> mDatas) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setWindowAnimations(R.style.ChooseStyle);
        View contentView = View.inflate(context, R.layout.dialog_price, null);
        setContentView(contentView);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.BOTTOM);
        ButterKnife.bind(this, contentView);


        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8), false));
        mAdapter = new PriceAdapter(mDatas);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            AppPrice appPrice = mAdapter.getItem(position);
            for (AppPrice a : mAdapter.getData()) {
                a.setSelect(false);
            }
            appPrice.setSelect(true);
            mAdapter.notifyDataSetChanged();
        });


    }

    @OnClick({R.id.tv_close, R.id.submit, R.id.rule})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_close:
                hide();
                break;
            case R.id.rule:
//                mContext.startActivity(new Intent(mContext, MyVipActivity.class));
                MyVipActivity.start(mContext,true);

                break;
            case R.id.submit:
                if (mAdapter.getSelect() == null) {
                    XToast.toast(mContext, "请选择规格");
                } else {
                    if (call != null) {
                        call.select(mAdapter.getSelect());
                        mAdapter.notifyDataSetChanged();
                    }
                    hide();
                }

                break;
        }
    }

    SelectCall call;

    public void addSelectCall(SelectCall call) {
        this.call = call;
    }
}
